/******************************************************************************/
/* oLED.c																	  */
/* Chris Day, Ying Guo	                                                      */
/******************************************************************************/
#include "rit128x96x4.h"
#include <stdio.h>
#include <string.h>
#include "OS.h"
#include "oLED.h"

/***** Global Variables *****/
unsigned char CONTRAST;
Sema4Type Done;

/******** oLED_Init *********************************************************/
/* Initialize oLED, set default contrast to highest							*/
/****************************************************************************/
void oLED_Init(void) {
  RIT128x96x4Init(1000000);
  RIT128x96x4Clear();
  OS_InitSemaphore (&Done, 1);

  CONTRAST = 15;
}

/******** oLED_Contrast *****************************************************/
/* Set the contrast, 0-15 with 15 being the highest							*/
/****************************************************************************/
void oLED_Contrast(unsigned char level) {
  CONTRAST = level;
}

/******** oLED_String *********************************************************/
/* oLED with two logically separate displays.								  */
/* \Device specifies top(0) or bottom(1);									  */
/* \Line specifies the line number (0 to 4);								  */
/* \String is a pointer null terminated ASCII string;						  */
/* \return None.															  */
/******************************************************************************/
void oLED_String (int device, int line, char *string) {
    unsigned long ulY;

	if ((device > 1)||(line > 5)) {
		return;
	}

	if (device == 0) {
		ulY = line * 9;
	} else {
		ulY = line * 9 + 48;
	}
	
	OS_Wait(&Done);

	RIT128x96x4StringDraw(string, 0, ulY, CONTRAST);

	OS_Signal(&Done);		
}

/******** oLED_Message ********************************************************/
/* oLED with two logically separate displays.								  */
/* \Device specifies top(0) or bottom(1);									  */
/* \Line specifies the line number (0 to 4);								  */
/* \String is a pointer null terminated ASCII string;						  */
/* \Value is a number to display.											  */
/* \return None.															  */
/******************************************************************************/
void oLED_Message (int device, int line, char *string, unsigned long value) {
    unsigned long ulY;
	char lString[22];

	if ((device > 1)||(line > 5)) {
		return;
	}

	sprintf ( lString, "%s%d", string, value );

	if (device == 0) {
		ulY = line * 9;
	} else {
		ulY = line * 9 + 48;
	}
	
	OS_Wait(&Done);

	RIT128x96x4StringDraw(lString, 0, ulY, CONTRAST);

	OS_Signal(&Done);		
}
